#import "TSHRootViewController.h"
#import <TSUtil.h>
#import <TSPresentationDelegate.h>

@implementation TSHRootViewController

- (BOOL)isTrollStore
{
	return NO;
}

- (void)viewDidLoad
{
	[super viewDidLoad];
	TSPresentationDelegate.presentationViewController = self;

	[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadSpecifiers) name:UIApplicationWillEnterForegroundNotification object:nil];

	// fetchLatestTrollStoreVersion(^(NSString* latestVersion)
	// {
	// 	NSString* currentVersion = [self getTrollStoreVersion];
	// 	NSComparisonResult result = [currentVersion compare:latestVersion options:NSNumericSearch];
	// 	if(result == NSOrderedAscending)
	// 	{
	// 		_newerVersion = latestVersion;
	// 		dispatch_async(dispatch_get_main_queue(), ^
	// 		{
	// 			[self reloadSpecifiers];
	// 		});
	// 	}
	// });
}

- (NSMutableArray*)specifiers
{
	if(!_specifiers)
	{
		_specifiers = [NSMutableArray new];

		#ifdef LEGACY_CT_BUG
		NSString* credits = @"您当前的手机系统支持安装！";
		#else
		NSString* credits = @"您当前的手机系统支持安装。";

		#endif

		PSSpecifier* infoGroupSpecifier = [PSSpecifier emptyGroupSpecifier];
		infoGroupSpecifier.name = @"Info";
		[_specifiers addObject:infoGroupSpecifier];

		PSSpecifier* infoSpecifier = [PSSpecifier preferenceSpecifierNamed:@"巨魔安装器"
											target:self
											set:nil
											get:@selector(getTrollStoreInfoString)
											detail:nil
											cell:PSTitleValueCell
											edit:nil];
		infoSpecifier.identifier = @"info";
		[infoSpecifier setProperty:@YES forKey:@"enabled"];

		[_specifiers addObject:infoSpecifier];

		BOOL isInstalled = trollStoreAppPath();

		// if(_newerVersion && isInstalled)
		// {
		// 	// Update TrollStore
		// 	PSSpecifier* updateTrollStoreSpecifier = [PSSpecifier preferenceSpecifierNamed:[NSString stringWithFormat:@"Update TrollStore to %@", _newerVersion]
		// 								target:self
		// 								set:nil
		// 								get:nil
		// 								detail:nil
		// 								cell:PSButtonCell
		// 								edit:nil];
		// 	updateTrollStoreSpecifier.identifier = @"updateTrollStore";
		// 	[updateTrollStoreSpecifier setProperty:@YES forKey:@"enabled"];
		// 	updateTrollStoreSpecifier.buttonAction = @selector(updateTrollStorePressed);
		// 	[_specifiers addObject:updateTrollStoreSpecifier];
		// }

		PSSpecifier* lastGroupSpecifier;

		PSSpecifier* utilitiesGroupSpecifier = [PSSpecifier emptyGroupSpecifier];
		[_specifiers addObject:utilitiesGroupSpecifier];

		lastGroupSpecifier = utilitiesGroupSpecifier;

		if(isInstalled || trollStoreInstalledAppContainerPaths().count)
		{
			PSSpecifier* refreshAppRegistrationsSpecifier = [PSSpecifier preferenceSpecifierNamed:@"刷新应用"
												target:self
												set:nil
												get:nil
												detail:nil
												cell:PSButtonCell
												edit:nil];
			refreshAppRegistrationsSpecifier.identifier = @"refreshAppRegistrations";
			[refreshAppRegistrationsSpecifier setProperty:@YES forKey:@"enabled"];
			refreshAppRegistrationsSpecifier.buttonAction = @selector(refreshAppRegistrationsPressed);
			[_specifiers addObject:refreshAppRegistrationsSpecifier];
		}
		if(isInstalled)
		//备注：如果安装
		{
			PSSpecifier* uninstallTrollStoreSpecifier = [PSSpecifier preferenceSpecifierNamed:@"卸载安装器"
										target:self
										set:nil
										get:nil
										detail:nil
										cell:PSButtonCell
										edit:nil];
			uninstallTrollStoreSpecifier.identifier = @"uninstallTrollStore";
			[uninstallTrollStoreSpecifier setProperty:@YES forKey:@"enabled"];
			[uninstallTrollStoreSpecifier setProperty:NSClassFromString(@"PSDeleteButtonCell") forKey:@"cellClass"];
			uninstallTrollStoreSpecifier.buttonAction = @selector(uninstallTrollStorePressed);
			[_specifiers addObject:uninstallTrollStoreSpecifier];
		}
		else
		{
			PSSpecifier* installTrollStoreSpecifier = [PSSpecifier preferenceSpecifierNamed:@"安装巨魔安装器"
												target:self
												set:nil
												get:nil
												detail:nil
												cell:PSButtonCell
												edit:nil];
			installTrollStoreSpecifier.identifier = @"installTrollStore";
			[installTrollStoreSpecifier setProperty:@YES forKey:@"enabled"];
			installTrollStoreSpecifier.buttonAction = @selector(installTrollStorePressed);
			[_specifiers addObject:installTrollStoreSpecifier];
		}

		NSString* backupPath = [getExecutablePath() stringByAppendingString:@"_TROLLSTORE_BACKUP"];
		if([[NSFileManager defaultManager] fileExistsAtPath:backupPath])
		{
			PSSpecifier* uninstallHelperGroupSpecifier = [PSSpecifier emptyGroupSpecifier];
			[_specifiers addObject:uninstallHelperGroupSpecifier];
			lastGroupSpecifier = uninstallHelperGroupSpecifier;

			PSSpecifier* uninstallPersistenceHelperSpecifier = [PSSpecifier preferenceSpecifierNamed:@"Uninstall Persistence Helper"
												target:self
												set:nil
												get:nil
												detail:nil
												cell:PSButtonCell
												edit:nil];
			uninstallPersistenceHelperSpecifier.identifier = @"uninstallPersistenceHelper";
			[uninstallPersistenceHelperSpecifier setProperty:@YES forKey:@"enabled"];
			[uninstallPersistenceHelperSpecifier setProperty:NSClassFromString(@"PSDeleteButtonCell") forKey:@"cellClass"];
			uninstallPersistenceHelperSpecifier.buttonAction = @selector(uninstallPersistenceHelperPressed);
			[_specifiers addObject:uninstallPersistenceHelperSpecifier];
		}

		#ifdef EMBEDDED_ROOT_HELPER
		// LSApplicationProxy* persistenceHelperProxy = findPersistenceHelperApp(PERSISTENCE_HELPER_TYPE_ALL);
		// BOOL isRegistered = [persistenceHelperProxy.bundleIdentifier isEqualToString:NSBundle.mainBundle.bundleIdentifier];

		// if((isRegistered || !persistenceHelperProxy) && ![[NSFileManager defaultManager] fileExistsAtPath:@"/Applications/TrollStorePersistenceHelper.app"])
		// {
		// 	PSSpecifier* registerUnregisterGroupSpecifier = [PSSpecifier emptyGroupSpecifier];
		// 	lastGroupSpecifier = nil;

		// 	NSString* bottomText;
		// 	PSSpecifier* registerUnregisterSpecifier;

		// 	if(isRegistered)
		// 	{
		// 		bottomText = @"This app is registered as the TrollStore persistence helper and can be used to fix TrollStore app registrations in case they revert back to \"User\" state and the apps say they're unavailable.";
		// 		registerUnregisterSpecifier = [PSSpecifier preferenceSpecifierNamed:@"Unregister Persistence Helper"
		// 										target:self
		// 										set:nil
		// 										get:nil
		// 										detail:nil
		// 										cell:PSButtonCell
		// 										edit:nil];
		// 		registerUnregisterSpecifier.identifier = @"registerUnregisterSpecifier";
		// 		[registerUnregisterSpecifier setProperty:@YES forKey:@"enabled"];
		// 		[registerUnregisterSpecifier setProperty:NSClassFromString(@"PSDeleteButtonCell") forKey:@"cellClass"];
		// 		registerUnregisterSpecifier.buttonAction = @selector(unregisterPersistenceHelperPressed);
		// 	}
		// 	else if(!persistenceHelperProxy)
		// 	{
		// 		bottomText = @"如果您想使用此应用程序作为 TrollStore 持久性助手，您可以在此处注册。";
		// 		registerUnregisterSpecifier = [PSSpecifier preferenceSpecifierNamed:@"注册持久化助手"
		// 										target:self
		// 										set:nil
		// 										get:nil
		// 										detail:nil
		// 										cell:PSButtonCell
		// 										edit:nil];
		// 		registerUnregisterSpecifier.identifier = @"registerUnregisterSpecifier";
		// 		[registerUnregisterSpecifier setProperty:@YES forKey:@"enabled"];
		// 		registerUnregisterSpecifier.buttonAction = @selector(registerPersistenceHelperPressed);
		// 	}

		// 	[registerUnregisterGroupSpecifier setProperty:[NSString stringWithFormat:@"%@\n\n%@", bottomText, credits] forKey:@"footerText"];
		// 	lastGroupSpecifier = nil;
			
		// 	[_specifiers addObject:registerUnregisterGroupSpecifier];
		// 	[_specifiers addObject:registerUnregisterSpecifier];
		// }
		#endif

if (lastGroupSpecifier)
{
    // 设置 lastGroupSpecifier 的 footerText
    [lastGroupSpecifier setProperty:credits forKey:@"footerText"];

    // 添加一段额外的文字消息
    NSString *additionalText = @"如果您的安装器无法打开，请点击「刷新应用」后，重新打开即可";
    
    PSSpecifier *additionalSpecifier = [PSSpecifier emptyGroupSpecifier];
    [additionalSpecifier setProperty:additionalText forKey:@"footerText"];
    
    [_specifiers addObject:additionalSpecifier];
}
	}
	//备注：顶部标题名
	[(UINavigationItem *)self.navigationItem setTitle:@"用力巨魔安装器"];
	return _specifiers;
}

//备注：判断是否安装
- (NSString*)getTrollStoreInfoString
{
	NSString* version = [self getTrollStoreVersion];
	if(!version)
	{
		
		return @"未安装";
	}
	else
	{
		return [NSString stringWithFormat:@"已安装, %@", version];
	}
}

- (void)handleUninstallation
{
	_newerVersion = nil;
	[super handleUninstallation];
}

- (void)registerPersistenceHelperPressed
{
	int ret = spawnRoot(rootHelperPath(), @[@"register-user-persistence-helper", NSBundle.mainBundle.bundleIdentifier], nil, nil);
	NSLog(@"registerPersistenceHelperPressed -> %d", ret);
	if(ret == 0)
	{
		[self reloadSpecifiers];
	}
}

- (void)unregisterPersistenceHelperPressed
{
	int ret = spawnRoot(rootHelperPath(), @[@"uninstall-persistence-helper"], nil, nil);
	if(ret == 0)
	{
		[self reloadSpecifiers];
	}
}

@end
